﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGetItemService" in both code and config file together.
[ServiceContract]
public interface IGetItemService
{
    [OperationContract]
    List<Item> getAll(String item_Name, Guid categoryID, Guid userID, float startBid, float endBid, int number);

    [OperationContract]
    Item getItemByID(Guid itemID);

    [OperationContract]
    List<Category> getAllCategory();
}

[DataContract]
public class Category
{
    Guid category_ID;
    [DataMember]
    public Guid Category_ID
    {
        get { return category_ID; }
        set { category_ID = value; }
    }

    String category_Name;
    [DataMember]
    public String Category_Name
    {
        get { return category_Name; }
        set { category_Name = value; }
    }

    bool category_Status;
    [DataMember]
    public bool Category_Status
    {
        get { return category_Status; }
        set { category_Status = value; }
    }

    Guid category_Father;
    [DataMember]
    public Guid Category_Father
    {
        get { return category_Father; }
        set { category_Father = value; }
    }
    public Category(Category_Table ct)
    {
        category_ID = ct.CategoryID;
        Category_Name = ct.CategoryName;
        category_Status = ct.CategoryStatus;
        if (ct.CategoryFather != null)
        {
            category_Father = (Guid)ct.CategoryFather;
        }
        else
        {
            category_Father = default(Guid);
        }
    }
    public Category()
    {
    }
}

[DataContract]
[KnownType(typeof(Category))]
public class Item : Category
{
    Guid itemID;
    [DataMember]
    public Guid ItemID
    {
        get { return itemID; }
        set { itemID = value; }
    }

    String itemTile;
    [DataMember]
    public String ItemTitle
    {
        get { return itemTile; }
        set { itemTile = value; }
    }

    String item_Decription;
    [DataMember]
    public String Item_Decription
    {
        get { return item_Decription; }
        set { item_Decription = value; }
    }

    String item_Image;
    [DataMember]
    public String Item_Image
    {
        get { return item_Image; }
        set { item_Image = value; }
    }

    String item_Tag;
    [DataMember]
    public String Item_Tag
    {
        get { return item_Tag; }
        set { item_Tag = value; }
    }

    bool bid_Status;
    [DataMember]
    public bool Bid_Status
    {
        get { return bid_Status; }
        set { bid_Status = value; }
    }

    DateTime bid_StartDate;
    [DataMember]
    public DateTime Bid_StartDate
    {
        get { return bid_StartDate; }
        set { bid_StartDate = value; }
    }

    DateTime bid_EndDate;
    [DataMember]
    public DateTime Bid_EndDate
    {
        get { return bid_EndDate; }
        set { bid_EndDate = value; }
    }

    int bid_Incremenent;
    [DataMember]
    public int Bid_Incremenent
    {
        get { return bid_Incremenent; }
        set { bid_Incremenent = value; }
    }

    float minimum_Bid;
    [DataMember]
    public float Minimum_Bid
    {
        get { return minimum_Bid; }
        set { minimum_Bid = value; }
    }
    
    List<Image> listImage;
    [DataMember]
    public List<Image> ListImage
    {
        get { return listImage; }
        set { listImage = value; }
    }

    List<Auction> listAuction;
    [DataMember]
    public List<Auction> ListAuction
    {
        get { return listAuction; }
        set { listAuction = value; }
    }

    Auction currentAuction;
    [DataMember]
    public Auction CurrentAuction
    {
        get { return currentAuction; }
        set { currentAuction = value; }
    }

    float currentPrice;
    [DataMember]
    public float CurrentPrice
    {
        get { return currentPrice; }
        set { currentPrice = value; }
    }

    Guid userID;
    [DataMember]
    public Guid UserID
    {
        get { return userID; }
        set { userID = value; }
    }

    TimeSpan timeSpan;
    [DataMember]
    public TimeSpan TimeSpan
    {
        get { return timeSpan; }
        set { timeSpan = value; }
    }

    public Item(Item_Table itemCopy)
    {
        itemID = itemCopy.Item_ID;
        itemTile = itemCopy.Item_Title;
        item_Decription = itemCopy.Item_Description;
        item_Image = itemCopy.Item_Image;
        item_Tag = itemCopy.Item_Tag;
        bid_Status = (bool)itemCopy.Bid_Status;
        bid_StartDate = (DateTime)itemCopy.Bid_StartDate;
        bid_EndDate = (DateTime)itemCopy.Bid_EndDat;
        bid_Incremenent = (int)itemCopy.Bid_Incremenent;
        minimum_Bid = (float)itemCopy.Minimum_Bid;
        Category_ID = (Guid)itemCopy.Category_ID;
        Category_Name = itemCopy.Category_Table.CategoryName;
        Category_Status = itemCopy.Category_Table.CategoryStatus;
        Category_Father = (Guid)itemCopy.Category_Table.CategoryFather;
        userID = (Guid)itemCopy.UserSell_ID;
        
        //find max amount
        DataClassesDataContext db = new DataClassesDataContext();
        if (db.Auction_Tables.ToList().Count > 0)
        {
            DateTime maxDateTime = db.Auction_Tables.Where(q => q.ItemID.Equals(itemID)).Max(q => q.Bid_Time);
            Auction_Table auc = db.Auction_Tables.Where(q => (q.Bid_Time.Equals(maxDateTime))).First();
            currentAuction = new Auction(auc);
            currentPrice = currentAuction.Bid_Amount;
        }
        else
        {
            currentPrice = minimum_Bid;
        }
        // find timestand
        DateTime now = new DateTime();
        timeSpan = bid_EndDate - now;
    }
    // get all image
    public void getAllImage()
    {
        listImage = new List<Image>();
        DataClassesDataContext db = new DataClassesDataContext();
        var ims = db.Image_Tables.Where(q => q.Item_ID.Equals(itemID));
        foreach (Image_Table im in ims)
        {
            listImage.Add(new Image(im));
        }
    }
    // get all auction
    public void getAllAuction()
    {
        listAuction = new List<Auction>();
        DataClassesDataContext db = new DataClassesDataContext();
        var ims = db.Auction_Tables.Where(q => q.ItemID.Equals(itemID));
        foreach (Auction_Table im in ims)
        {
            listAuction.Add(new Auction(im));
        }
    }
}

[DataContract]
public class Image
{
    Guid image_ID;
    [DataMember]
    public Guid Image_ID
    {
        get { return image_ID; }
        set { image_ID = value; }
    }

    String image_Title;
    [DataMember]
    public String Image_Title
    {
        get { return image_Title; }
        set { image_Title = value; }
    }

    String image_URL;
    [DataMember]
    public String Image_URL
    {
        get { return image_URL; }
        set { image_URL = value; }
    }

    String image_Decription;
    [DataMember]
    public String Image_Decription
    {
        get { return image_Decription; }
        set { image_Decription = value; }
    }

    public Image(Image_Table imageCopy)
    {
        image_ID = imageCopy.Image_ID;
        image_Title = imageCopy.Image_Title;
        image_URL = imageCopy.Image_URL;
        image_Decription = imageCopy.Image_Description;
    }
}

[DataContract]
public class Auction
{
    Guid auctionID;
    [DataMember]
    public Guid AuctionID
    {
        get { return auctionID; }
        set { auctionID = value; }
    }

    DateTime bid_Time;
    [DataMember]
    public DateTime Bid_Time
    {
        get { return bid_Time; }
        set { bid_Time = value; }
    }

    float bid_Amount;
    [DataMember]
    public float Bid_Amount
    {
        get { return bid_Amount; }
        set { bid_Amount = value; }
    }

    public Auction(Auction_Table auctionCopy)
    {
        auctionID = auctionCopy.AuctionID;
        bid_Time = auctionCopy.Bid_Time;
        bid_Amount = auctionCopy.Bid_Amount;
    }
}
